<?php
DEFINE("XML_PROLOGUE", '<?xml version="1.0" encoding="UTF-8"?>');
DEFINE("NEWLINE", "\n\t");


function makeQueryXML($result) {
	$resultsize=0;
	if (!$result) {
		die("Something is Seriously wrong!!!!");
	} else {
		echo XML_PROLOGUE . "\n ";
		echo "<records>" . NEWLINE;
		while ($row = mysql_fetch_assoc($result)) {
			echo NEWLINE."<record>".NEWLINE;
			$resultsize++;
			foreach ($row as $key => $value) {
				echo "\t<" . $key . ">" . $value . "</" . $key . ">" . NEWLINE;
			}
			echo "</record>".NEWLINE;
		}
		echo NEWLINE . "<resultsize>" . $resultsize . "</resultsize>";
		echo "\n" . "</records>";
		
	}
}
function makeUpdateXML($result) {
	$resultsize=0;
	if (!$result) {
		die("Something is Seriously wrong!!!!");
	} else {
		echo XML_PROLOGUE . "\n ";
		echo "<result>".$result."</result>";
	}
}
function generateResultSet($strQuery){
	$result = mysql_query($strQuery);
	if (!$result) {
		die("SQL query failed");
	}
	return $result;
}

function makePlayerStatsXML($result) {
	$types = array(array("T20",1),array("ODI",1),array("TEST",1));
	$resultsize=0;
	
	while ($row = mysql_fetch_assoc($result)) {
		echo "<record>".NEWLINE;
		$resultsize++;
		foreach ($row as $key => $value) {
			echo "\t<" . $key . ">" . $value . "</" . $key . ">" . NEWLINE;
		}
		echo "</record>".NEWLINE;
		for ($i = 0; $i < 3; $i++) {
			if($types[$i][0]==$row["statType"]){
				$types[$i][1]=0;
			}
		}
	}
	for ($i = 0; $i < 3; $i++) {
		if($types[$i][1]==1){
			echo "<record>".NEWLINE;
			$resultsize++;
			makeEmptyStatRecord('stat','statType',$types[$i][0]);
			echo "</record>".NEWLINE;
		}
	}
}
function makeEmptyStatRecord($tableName,$missingField,$value){
	$cols = mysql_query("SHOW COLUMNS FROM ".$tableName);
	while ($col = mysql_fetch_assoc($cols)) {
		if(strcasecmp($col['field'],"id")!=0){
			echo "\t<" . $col['Field']. ">";
			if(strcasecmp($col['Field'],$missingField)==0){
				echo $value;
			}else if(strpos($col['Type'], 'int')==4){
				echo 0;
			}else if (strpos($col['Type'], 'nt')==1) {
				echo 0;
			}else if (strpos($col['Type'], 'loat')==1) {
				echo 0.0;
			}
			echo "</" . $col['Field']. ">" . NEWLINE;
			
		}
	}
}
function makeTeamQueryXML($result) {
	$resultsize=0;
	if (!$result) {
		die("Something is Seriously wrong!!!!");
	} else {
		echo XML_PROLOGUE . "\n ";
		echo "<records>" . NEWLINE;
		while ($row = mysql_fetch_assoc($result)) {
			echo NEWLINE."<record>".NEWLINE;
			$resultsize++;
			echo NEWLINE."<id>".$row["id"]."</id>";
			echo NEWLINE."<name>".$row["id"]."</name>"; 
			echo NEWLINE."<userId>".$row["id"]."</userId>"; 
			echo NEWLINE."<fromDate>".$row["id"]."</fromDate>"; 
			echo NEWLINE."<toDate>".$row["id"]."</toDate>";
			$teamPlayers = Constants::$teamPlayers; 
			for ( $index = 0, $max_count = sizeof( $teamPlayers ); $index < $max_count; $index++ ) {
				$strQuery = "select id,fname,lname,playerType,country from player where id='" . $row[$teamPlayers[$index]."Id"] . "'";
				echo NEWLINE."<player>".NEWLINE;
				echo "\t<column>".$teamPlayers[$index]."</column>".NEWLINE;
				makePlayerXML(generateResultSet($strQuery));
				echo "</player>".NEWLINE;
			
			}
			/*$strQuery = "select id,fname from player where id='" . $row["Batsman1Id"] . "'";
			echo NEWLINE."<Batsman1>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Batsman1>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["Batsman2Id"] . "'";
			echo NEWLINE."<Batsman2>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Batsman2>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["Batsman3Id"] . "'";
			echo NEWLINE."<Batsman3>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Batsman3>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["Batsman4Id"] . "'";
			echo NEWLINE."<Batsman4>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Batsman4>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["Batsman5Id"] . "'";
			echo NEWLINE."<Batsman5>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Batsman5>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["WicketkeeperId"] . "'";
			echo NEWLINE."<Wicketkeeper>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Wicketkeeper>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["AllRounderId"] . "'";
			echo NEWLINE."<AllRounder>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</AllRounder>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["Bowler1Id"] . "'";
			echo NEWLINE."<Bowler1>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Bowler1>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["Bowler2Id"] . "'";
			echo NEWLINE."<Bowler2>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Bowler2>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["Bowler3Id"] . "'";
			echo NEWLINE."<Bowler3>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Bowler3>".NEWLINE;
			$strQuery = "select * from player where id='" . $row["Bowler4Id"] . "'";
			echo NEWLINE."<Bowler4>".NEWLINE;
			makePlayerXML(generateResultSet($strQuery));
			echo "</Bowler4>".NEWLINE;*/
			echo NEWLINE . "</record>".NEWLINE;
		}
		echo NEWLINE . "<resultsize>" . $resultsize . "</resultsize>";
		echo "\n" . "</records>";
		
	}
}
function makePlayerXML($result) {
	if (!$result) {
		die("Something is Seriously wrong!!!!");
	} else {
		while ($row = mysql_fetch_assoc($result)) {
			foreach ($row as $key => $value) {
				echo "\t<" . $key . ">" . $value . "</" . $key . ">" . NEWLINE;
			}
		}
		
	}
}
?>